package com.mediacloud.app.newsmodule.fragment.webview;

import android.text.TextUtils;

import java.net.*;
import java.util.*;

public class AnalysisURLUti
{
    /**
     * 获取Url字符串的参数
     *
     * @param url
     * @return
     */
    public static Map<String, String> getUrlParam(String url, boolean isDecode) {
        Map<String, String> paramMap = new HashMap<String, String>();
        if (TextUtils.isEmpty(url)) {
            return paramMap;
        }
        try {
            URI uri = new URI(url);
            String params = isDecode ? uri.getQuery() : uri.getRawQuery();
            if (params != null && !"".equals(params)) {
                String paramArray[] = params.split("&");
                if (paramArray != null && paramArray.length != 0) {
                    for (String pams : paramArray) {
                        String keyValuePams[] = pams.split("=");
                        if (keyValuePams != null && keyValuePams.length == 2) {
                            if (isDecode) {
                                paramMap.put(keyValuePams[0], URLDecoder.decode(keyValuePams[1], "UTF-8"));
                            } else {
                                paramMap.put(keyValuePams[0], keyValuePams[1]);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return paramMap;
    }

    /**
     * @param data
     * @return
     */
    public static String insertParam(Map<String, String> data) {
        StringBuffer buffer = new StringBuffer();
        if (data != null) {
            Set<String> strings = data.keySet();
            for (String key : strings) {
                if (!TextUtils.isEmpty(key)) {
                    String value = data.get(key);
                    String temParam = key + "=" + value + "&";
                    buffer.append(temParam);
                }
            }
            if (buffer.length() > 0) {
                buffer.deleteCharAt(buffer.length() - 1);
            }
        }
        return buffer.toString();
    }

    public static final String scheme = "wmsdk.n.weimob.com://";
}
